package com.ego.controller;

import com.ego.exception.EgoException;
import com.ego.exception.ExceptionEnum;
import com.ego.pojo.Category;
import com.ego.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Ares
 * @date 2020-11-20
 */
@Controller
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 根据parentId查询类目
     *
     * @param pid 父Id
     * @return 返回一个种类集合
     */
    @RequestMapping("/list")
    public ResponseEntity<List<Category>> queryCategoryListByParentId(@RequestParam(value = "pid", defaultValue = "0") Long pid) {
        try {
            if (pid == null || pid < 0) {
                // pid为null或者小于等于0，响应400
                return ResponseEntity.badRequest().build();
            }
            // 执行查询操作
            List<Category> categoryList = this.categoryService.queryCategoryListByParentId(pid);
            if (CollectionUtils.isEmpty(categoryList)) {
                // 返回结果集为空，响应404
                return ResponseEntity.notFound().build();
            }
            // 响应200
            return ResponseEntity.ok(categoryList);
        } catch (Exception e) {
            EgoException.error(log, ExceptionEnum.CATEGORY_NOT_FOUND,e);
        }
        return null;
    }

    @PostMapping
    public ResponseEntity<Void> save(@RequestBody Category category){
        Integer rows = categoryService.save(category);
        if(rows>0) {
            return ResponseEntity.ok().build();
        }
        return null;
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id){
        Integer rows = categoryService.delete(id);
        if(rows>0){
            return ResponseEntity.ok().build();
        }
        return null;
    }

    @PutMapping
    public ResponseEntity<Void> update(@RequestParam("id") Long id,@RequestParam("name") String name){
        Category category = new Category();
        category.setId(id);
        category.setName(name);
        Integer rows = categoryService.update(category);
        if(rows>0){
            return ResponseEntity.ok().build();
        }
        return null;
    }

    @GetMapping("/bid/{bid}")
    public ResponseEntity<List<Category>> getById(@PathVariable Long bid){
        List<Long> list = categoryService.selectById(bid);
        List<Category> categoryList = new ArrayList<>();
        for (Long aLong : list) {
            Category category = categoryService.selectCategoryById(aLong);
            categoryList.add(category);
        }
        return ResponseEntity.ok(categoryList);
    }

    @GetMapping("/list/ids")
    public ResponseEntity<List<Category>> selectByIds(@RequestParam("ids") List<Long> idList){
        List<Category> categories = categoryService.selectByIds(idList);
        return ResponseEntity.ok(categories);
    }

}
